import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import { refreshToken as refreshTokenApi } from '@/api/index/index'
import type { MemberInfo } from '@/types'
import { checkMemberMp, getUserRoleList } from '@/api/mine/userInfo'
import { checkChineseName } from '@/utils/Validate'

// 定义 Store
export const useMemberStore = defineStore(
  'member',
  () => {
    // 用户信息
    const userInfo = ref()
    // 会员信息
    const memberInfo = ref<MemberInfo>({
      nickname: '',
      avatar: '',
      userMobile: '',
      sex: '',
      idNumberFrontImage: '',
      idNumberBackImage: '',
      bankCardNumber: '',
      bankCardImage: ''
    })
    // 认证信息
    const authInfo = ref()
    // 判断是否登录
    const isLogin = computed(() => {
      return userInfo.value?.accessToken ? true : false
    })
    // 判断是否真实姓名
    const isRealName = computed(() => {
      if (!isLogin.value || checkChineseName(memberInfo.value.nickname)) {
        return true
      } else {
        return false
      }
    })
    // 判断是否有身份证号
    const isIdNumber = computed(() => {
      if (memberInfo.value.idNumber && memberInfo.value.certBeginDate && memberInfo.value.certEndDate) {
        return true
      } else {
        return false
      }
    })
    // 判断是否认证过
    const isAuth = computed(() => {
      if (authInfo?.value?.status == 1) {
        return true
      } else {
        return false
      }
    })
    // 查询是否关注公众号
    const checkMemberMpResult = ref(false)
    const checkMemberMpRes = async () => {
      const res: any = await checkMemberMp()
      if (res.code == 0) {
        checkMemberMpResult.value = res.data
      } else {
        checkMemberMpResult.value = false
      }
    }
    // 保存用户信息，登录时使用
    const setUserInfo = (val: any) => {
      userInfo.value = val
    }

    // 保存会员信息
    const setMemberInfo = (val: MemberInfo) => {
      memberInfo.value = val
    }
    // 保存审核信息
    const setAuthInfo = (val: any) => {
      authInfo.value = val
    }
    // 更新认证信息
    const updateAuthInfo = async () => {
      // 如果已经认证通过 则不再更新
      if (isAuth.value) return
      const role = uni.getStorageSync('role')
      if (role == 'worker') {
        const res: any = await getUserRoleList()
        if (res.code == 0 && res.data.length > 0) {
          const data = res.data[0]
          authInfo.value = data
        }
      }
    }

    // 清理所有信息，退出时使用(不清除角色信息)
    const clearUserInfo = () => {
      userInfo.value = undefined
      authInfo.value = undefined
      memberInfo.value = {
        nickname: '',
        avatar: '',
        userMobile: '',
        sex: '',
        idNumberFrontImage: '',
        idNumberBackImage: '',
        bankCardNumber: '',
        bankCardImage: ''
      }
    }

    // 刷新token
    const refreshToken = () => {
      refreshTokenApi(userInfo.value.refreshToken).then((res: any) => {
        userInfo.value.accessToken = res.data.accessToken
        userInfo.value.expiresTime = res.data.expiresTime
        userInfo.value.refreshToken = res.data.refreshToken
      })
    }

    // 记得 return
    return {
      userInfo,
      memberInfo,
      authInfo,
      isLogin,
      isRealName,
      isIdNumber,
      isAuth,
      checkMemberMpResult,
      checkMemberMpRes,
      setUserInfo,
      setMemberInfo,
      setAuthInfo,
      updateAuthInfo,
      clearUserInfo,
      refreshToken
    }
  },
  {
    // 网页端配置
    // persist: true,
    // 小程序端配置
    persist: {
      storage: {
        getItem(key) {
          return uni.getStorageSync(key)
        },
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
      },
    },
  },
)
